<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb>
        <el-breadcrumb-item>
          <i class="el-icon-tickets">货代详情</i>
        </el-breadcrumb-item>
      </el-breadcrumb>
      <div class="cantainer">
        <div class="handle-box">
          <el-button type="primary" @click="dialogFormVisible = true" class="handle-del mr10">添加</el-button>
          <el-button type="danger" @click="batchDelete" class="handle-del mr10">批量删除</el-button>
        </div>
        <el-table
          :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
          tooltip-effect="dark"
          style="width: 100%"
          v-loading="addLoading"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" ></el-table-column>
          <el-table-column
            prop="name"
            label="货代名称"
            sortable
            width="150px">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
              >编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="clickDel(scope.$index,scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="el-pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 40]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          background
          :total="list.length">
        </el-pagination>
      </div>
    </div>

    <!--编辑弹出框-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" width="20%" center>
      <el-form :model="editForm" label-width="50px" ref="addForm">
        <el-form-item label="货代名称" label-width="80px">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" :loading="addLoading" @click="editSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加弹出框 -->
    <el-dialog title="添加货代" :visible.sync="dialogFormVisible" width="20%" @close="closeAdd" center>
      <el-form :model="form" label-width="80px">
        <el-form-item label="货代名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeAdd">取 消</el-button>
        <el-button type="primary" @click="add" :loading="addLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "../../../util/axios_config";
import {getFreightForwardData} from "../../../util/common";

export default {
  data() {
    return {
      list: [], //数据存储
      dialogFormVisible: false,   //添加弹出框
      ListSearch: "",  //搜索
      editFormVisible: false,  //编辑器弹出框
      form: {    //需要添加的字段
        name: "",
      },
      editForm: {   //编辑需要的字段
        forwardId: "",
        name: ""
      },
      addLoading: false,
      currentPage: 1, //初始页
      pagesize: 5,    //	每页的数据
      delList: [],
      disabled:true
    }
  },
  watch: {
    ListSearch(val) {
      this.list.forEach(val => {

      })
    }
  },
  created() {
    this.vueTable();
  },
  methods: {
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pagesize = size;
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
    },
    // 获取货代信息
    vueTable() {
      this.addLoading = true;
      getFreightForwardData().then(data => {
        this.addLoading = false;
        this.list = data;
      }).catch(error => {
        this.addLoading = false;
        this.$message.error(error.message);
      });
    },
    // 显示编辑弹出框
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
      console.log(this.editForm)
    },
    // 编辑
    editSubmit(row) {
      if (!this.editForm.name || !this.editForm.forwardId) {
        this.$message.error('内容不能为空');
      } else {
        let HandleEdit = {
          forwardId: this.editForm.forwardId,
          name: this.editForm.name,
        }
        axios.put("freightForward/update", HandleEdit).then(res => {
          this.addLoading = false;
          this.editFormVisible = false;
          if (res.data) {
            var data = {};
            if (typeof res.data === 'string') {
              data = JSON.parse(res.data);
            } else {
              data = res.data;
            }
            if (data.errCode === 10000) {
              this.$message.success('添加成功');
            } else {
              this.$message.error(data.errMsg);
            }
          }
          this.vueTable();
        })
        console.log(HandleEdit)
      }

    },
    clickDel(index, row) {
      this.delList.push(row.forwardId);
      this.deleteList();
    },
    // 删除
    deleteList() {
      this.$confirm('此操作将永久删除该货代及渠道数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const uniqueArray = this.delList.filter((item, index) => this.delList.indexOf(item) === index);
        let HandleDel = {
          forwardIdAttr: uniqueArray,
        }
        axios.post("freightForward/del", HandleDel).then(res => {
          if (res.data) {
            var data = {};
            if (typeof res.data === 'string') {
              data = JSON.parse(res.data);
            } else {
              data = res.data;
            }
            if (data.errCode === 10000) {
              this.$message.success('删除成功');
              this.delList = [];
              this.vueTable()
            } else {
              this.$message.error(data.errMsg);
            }
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 添加数据
    add() {
      if (!this.form.name) {
        this.$message.error('内容不能为空');
      } else {
        this.addLoading = true;
        let handleAddClick = {
          name: this.form.name
        }
        axios.post("freightForward/addMsg", handleAddClick).then(res => {
          this.dialogFormVisible = false;
          this.addLoading = false;
          var data = {};
          if (typeof res.data === 'string') {
            data = JSON.parse(res.data);
          } else {
            data = res.data;
            if (data.errCode == 10000) {
              this.$message.success('添加成功')
              this.vueTable()
            } else {
              this.$message.error(data.errMsg)
            }
          }
        })
      }
    },
    //关闭添加表单
    closeAdd() {
      this.form.name = "";
      this.dialogFormVisible = false;
    },
    //多选框
    handleSelectionChange(val) {
      this.delList = val.map(item => item.forwardId);
    },
    //批量删除
    batchDelete(){
    if(this.delList.length<=0){
      this.$message.error('请选择要删除的数据');
    }else{
      this.deleteList()
    }
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.search-input {
  width: 200px;
  margin-left: 50px;
}

.el-table__body-wrapper {
  height: 500px;
}

.crumbs {
  margin-bottom: 20px;
}
.el-pagination {
  text-align: center;
}
</style>
